﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceManager.Utils;
using System.Diagnostics.Contracts;
namespace QuotationService.RealtimeQuotation
{
    
    public class StockQuotationRepository : QuotationRepositoryBase
    {
        private static StockEntityConverter stockEntityConverter = new StockEntityConverter();
        private static BasicInfoRepository<Quotation.Stock.BasicInfo, QuotationService.RealtimeQuotation.StockBasicInfo> basicInfoRepository = new BasicInfoRepository<Quotation.Stock.BasicInfo, StockBasicInfo>(stockEntityConverter);
        private static QuotationInfoRepository<Quotation.Stock.QuotationInfo, QuotationService.RealtimeQuotation.StockQuotationInfo> quotInfoRepository = new QuotationInfoRepository<Quotation.Stock.QuotationInfo, StockQuotationInfo>(stockEntityConverter);
        private static TickInfoRepository<Quotation.Stock.TickInfo, QuotationService.RealtimeQuotation.StockTickInfo> tickInfoRepository = new TickInfoRepository<Quotation.Stock.TickInfo, StockTickInfo>(stockEntityConverter);

        #region 基本信息操作
        /// <summary>
        /// 更新单个商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="basicInfo"></param>
        /// <returns></returns>
        public static bool UpdateBasicInfo(string quotSource, Quotation.Stock.BasicInfo basicInfo)
        {
            return basicInfoRepository.UpdateBasicInfo(quotSource, basicInfo);
        }
        /// <summary>
        /// 获取单个商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Quotation.Stock.BasicInfo GetBasicInfo(string quotSource, string code)
        {
            return basicInfoRepository.GetBasicInfo(quotSource, code);
        }
        /// <summary>
        /// 获取全部商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static List<Quotation.Stock.BasicInfo> GetAllBasicInfo(string quotSource)
        {
            return basicInfoRepository.GetAllBasicInfo(quotSource);
        }
        /// <summary>
        /// 清除单个商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool ClearBasicInfo(string quotSource, string code)
        {
            return basicInfoRepository.ClearBasicInfo(quotSource, code);
        }
        /// <summary>
        /// 清除全部商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static bool ClearAllBasicInfo(string quotSource)
        {
            return basicInfoRepository.ClearAllBasicInfo(quotSource);
        }
        #endregion

        #region 行情信息操作
        /// <summary>
        /// 更新单个商品的行情信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="quotInfo"></param>
        /// <param name="appendToHistoryList"></param>
        /// <returns></returns>
        public static bool UpdateQuotationInfo(string quotSource, Quotation.Stock.QuotationInfo quotInfo, bool appendToHistoryList)
        {
            return quotInfoRepository.UpdateQuotationInfo(quotSource, quotInfo, appendToHistoryList);
        }
       
        /// <summary>
        /// 获取单个商品的行情信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Quotation.Stock.QuotationInfo GetQuotationInfo(string quotSource, string code)
        {
            return quotInfoRepository.GetQuotationInfo(quotSource, code);
        }
        
        /// <summary>
        /// 获取全部商品的行情信息快照
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static List<Quotation.Stock.QuotationInfo> GetQuotationInfoSnap(string quotSource)
        {
            return quotInfoRepository.GetQuotationInfoSnap(quotSource);
        }
        /// <summary>
        /// 获取单个商品的当日历史行情
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static List<Quotation.Stock.QuotationInfo> GetHistoryQuotationInfo(string quotSource, string code)
        {
            return quotInfoRepository.GetHistoryQuotationInfo(quotSource, code);
        }
        /// <summary>
        /// 清除单个商品的行情信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool ClearQuotionInfo(string quotSource, string code)
        {
            return quotInfoRepository.ClearQuotionInfo(quotSource, code);
        }
        /// <summary>
        /// 清除全部商品的行情信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static bool ClearAll(string quotSource)
        {
            return quotInfoRepository.ClearAll(quotSource);
        }
        #endregion

        #region Tick信息操作
        /// <summary>
        /// 发布Tick
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <param name="tick"></param>
        /// <returns></returns>
        public static bool AppendTick(string quotSource, string code, Quotation.Stock.TickInfo tick)
        {
            return tickInfoRepository.AppendTick(quotSource, code, tick);
        }
        /// <summary>
        /// 获取单个商品的当日历史Tick
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <returns></returns>
        public static List<Quotation.Stock.TickInfo> GetTicks(string quotSource, string code, int? startIndex, int? endIndex)
        {
            return tickInfoRepository.GetTicks(quotSource, code, startIndex, endIndex);
        }

        #endregion

        #region 分线数据操作
        /// <summary>
        /// 更新单个商品的分线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="minuteData"></param>
        /// <returns></returns>
        public static bool UpdateMinuteData(string quotSource, Quotation.Stock.MinuteData minuteData)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(minuteData != null);
            StockMinuteData stockMinuteData = stockEntityConverter.Convert(minuteData);
            byte[] bytes = stockMinuteData.StructToBytes<StockMinuteData>();
            return MinuteDataChannel.Update(quotSource, stockMinuteData.Code, stockMinuteData.OccurTime, bytes);
        }
        /// <summary>
        /// 获取单个商品当日历史分线
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static List<Quotation.Stock.MinuteData> GetMinuteData(string quotSource, string code)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(!string.IsNullOrEmpty(code));
            List<byte[]> minuteBytesList = MinuteDataChannel.GetAll(quotSource, code);
            List<Quotation.Stock.MinuteData> rtn = new List<Quotation.Stock.MinuteData>();
            if (minuteBytesList != null)
            {
                foreach (var bytes in minuteBytesList)
                {
                    StockMinuteData stockMinuteData = StructHelper.BytesToStruct<StockMinuteData>(bytes);
                    rtn.Add(stockEntityConverter.Convert(stockMinuteData));
                }
            }
            return rtn;
        }
        /// <summary>
        /// 获取全部商品的分线数据快照
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static List<Quotation.Stock.MinuteData> GetMinuteDataSnap(string quotSource)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            List<byte[]> minuteBytesList = MinuteDataChannel.GetAll(quotSource);
            List<Quotation.Stock.MinuteData> rtn = new List<Quotation.Stock.MinuteData>();
            if (minuteBytesList != null)
            {
                foreach (var bytes in minuteBytesList)
                {
                    StockMinuteData stockMinuteData = StructHelper.BytesToStruct<StockMinuteData>(bytes);
                    rtn.Add(stockEntityConverter.Convert(stockMinuteData));
                }
            }
            return rtn;
        }
        /// <summary>
        /// 清除单个商品的全部分线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool ClearMinuteData(string quotSource, string code)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(!string.IsNullOrEmpty(code));
            return MinuteDataChannel.Clear(quotSource, code);
        }
        /// <summary>
        /// 清除全部商品的最新分线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static bool ClearMinuteDataSnap(string quotSource)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            return MinuteDataChannel.ClearAll(quotSource);
        }
        #endregion

        #region 日线数据操作
        /// <summary>
        /// 更新单个商品的日线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="dailyData"></param>
        /// <returns></returns>
        public static bool UpdateDailyData(string quotSource, Quotation.Stock.DailyData dailyData)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(dailyData != null);
            StockDailyData stockDailyData = stockEntityConverter.Convert(dailyData);
            byte[] bytes = stockDailyData.StructToBytes<StockDailyData>();
            return DailyDataChannel.Update(quotSource, dailyData.StkId.LongCode, bytes);
        }
        /// <summary>
        /// 获取单个商品的最新日线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Quotation.Stock.DailyData GetDailyData(string quotSource, string code)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(!string.IsNullOrEmpty(code));
            byte[] bytes = DailyDataChannel.Get(quotSource, code);
            if (bytes != null && bytes.Length > 0)
            {
                StockDailyData stockDailyData = StructHelper.BytesToStruct<StockDailyData>(bytes);
                return stockEntityConverter.Convert(stockDailyData);
            }
            else
                return null;
        }
        /// <summary>
        /// 获取全部商品的最新日线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static List<Quotation.Stock.DailyData> GetDailyDataSnap(string quotSource)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            List<byte[]> dailyBytesList = DailyDataChannel.GetAll(quotSource);
            List<Quotation.Stock.DailyData> rtn = new List<Quotation.Stock.DailyData>();
            if (dailyBytesList != null)
            {
                foreach (var bytes in dailyBytesList)
                {
                    StockDailyData stockDailyData = StructHelper.BytesToStruct<StockDailyData>(bytes);
                    rtn.Add(stockEntityConverter.Convert(stockDailyData));
                }
            }
            return rtn;
        }
        /// <summary>
        /// 清除单个商品的日线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool ClearDailyData(string quotSource, string code)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            Contract.Requires(!string.IsNullOrEmpty(code));
            return DailyDataChannel.Clear(quotSource, code);
        }
        /// <summary>
        /// 清除全部商品的日线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static bool ClearDailyDataSnap(string quotSource)
        {
            Contract.Requires(!string.IsNullOrEmpty(quotSource));
            return DailyDataChannel.ClearAll(quotSource);
        }
        #endregion

    }
}
